package com.akk.module.authentication;

import com.akk.exception.RestBusinessException;
import com.akk.module.authentication.model.request.AuthRequest;
import com.akk.module.authentication.model.request.BankCardAuthRequest;
import com.akk.module.authentication.model.response.AuthResponse;
import com.akk.module.authentication.model.response.BankCardAuthResponse;
import com.akk.utils.Constants;
import com.akk.utils.RestResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;


@Component
public class BankCardAuthService extends AuthService {
    private Logger logger = LoggerFactory.getLogger(BankCardAuthService.class);
    private static BankCardAuthService bankCardAuthService;

    public static BankCardAuthService getInstance() {
        if(bankCardAuthService == null)
            bankCardAuthService = new BankCardAuthService();
        return bankCardAuthService;
    }

    @Override
    public AuthResponse auth(AuthRequest request) {
        BankCardAuthRequest bankCardAuthRequest = (BankCardAuthRequest) request;
        //6.请求接口URL
        String url = "";//银行卡三要素
        String APP_KEY = "";//APP KEY
        String PRIVATE_KEY = "";//PRIVATE KEY

        if (bankCardAuthRequest.getType() == 3){
            // 三要素
            url = Constants.AUTH_THREE_URL;
            APP_KEY = Constants.AUTH_THREE_APP_KEY;
            PRIVATE_KEY = Constants.AUTH_THREE_PRIVATE_KEY;
        }else {
            // 四要素
            url = Constants.AUTH_FOUR_URL;
            APP_KEY = Constants.AUTH_FOUR_APP_KEY;
            PRIVATE_KEY = Constants.AUTH_FOUR_PRIVATE_KEY;
        }

        /** 生成请求报文**/
        //组装请求报文
        JSONObject content = new JSONObject();
        content.put("realName", bankCardAuthRequest.getRealName());//姓名
        content.put("idCard", bankCardAuthRequest.getIdCard());//身份证号码
        content.put("bankCard", bankCardAuthRequest.getBankCard());//银行卡号码
        content.put("mobile", bankCardAuthRequest.getMobile());//手机号码号码

        JSONObject json = getAuthMessage(
                Constants.AUTH_USER_CODE,
                Constants.AUTH_APP_CODE,
                bankCardAuthRequest.getBankCardReason(),
                content
        );

        JSONObject result = sendEncMessage(
                json.toString(),
                url,
                APP_KEY,
                Constants.AUTH_VECTOR,
                Constants.AUTH_USER_CODE,
                PRIVATE_KEY
        );
        if (result.has("data"))
        {
            JSONArray jsonArray = result.getJSONArray("data");
            if (jsonArray.size() > 0)
            {
                JSONObject jsonObject = jsonArray.getJSONObject(0);
                JSONArray recordArray = jsonObject.getJSONArray("record");
                JSONObject recordObject = recordArray.getJSONObject(0);
                return (AuthResponse) JSONObject.toBean(jsonObject, BankCardAuthResponse.class);
            }
        }
        //错误报文
        JSONObject msgObject = result.getJSONObject("msg");
        logger.error("银行卡实名认证出错.-->状态码:"+msgObject.get("code")+"/详情:"+msgObject.get("codeDesc"));
        throw new RestBusinessException("实名认证失败.");
    }

}
